package com.ruoyi.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.KongAdmin;

import org.springframework.beans.factory.annotation.Autowired;

import java.net.MalformedURLException;
import java.net.URL;

@org.springframework.stereotype.Service("ServiceKong")
public class ServiceKong {
    @Autowired
    KongAdmin kongAdmin;
    public String createService(JSONObject requestBody) {
        JSONObject serviceBody = requestBody.getJSONObject("service");
        JSONObject aclBody = requestBody.getJSONObject("acl");
        String result = kongAdmin.CreateService(serviceBody);
        String resultacl;
        String resultkey;
        JSONObject returnJson = new JSONObject();
        JSONObject serviceresult = JSON.parseObject(result);
        System.out.println(serviceresult.get("status"));
       System.out.println(serviceresult.get("status").toString().equals("201"));
        if (check(serviceresult)) {
            returnJson.put("service", "succeed");
            resultacl = kongAdmin.CreatePluginInService(aclBody, serviceBody.getString("name"), "acl");
            JSONObject aclresult = JSON.parseObject(resultacl);
           // System.out.println(aclresult.get("status"));
            JSONObject keyboy = new JSONObject();
            keyboy.put("name", "key-auth");
            resultkey = kongAdmin.CreatePluginInService(keyboy, serviceBody.getString("name"), "key-auth");
          //  System.out.println(resultkey);
            if (check(aclresult)) {
                returnJson.put("acl", "succeed");
                return returnJson.toJSONString();
            } else {
                returnJson.put("acl", "fail");
            }
        } else {
            returnJson.put("service", "fail");
        }
        return returnJson.toJSONString();
    }

    public String createServiceroute(JSONObject requestBody, String serviceName) {

        String re = kongAdmin.CreateRoute(requestBody, serviceName);
        JSONObject result = JSON.parseObject(re);
        if (check(result)) {
            return "succeed";
        } else {
            return "fail";
        }


    }

    private boolean check(JSONObject checkBody) {
      //  System.out.println(((checkBody.get("status").equals("201") )));
        if ((checkBody.get("status").toString().equals("200") )||( checkBody.get("status").toString().equals("201"))) {

            return true;
        }else{
            return false;
        }

    }

           public JSONObject patchServiceHost(String serviceName, String url) {
               JSONObject requestBody = new JSONObject();
               JSONObject resp=new JSONObject();
               try {
                   new URL(url);
                   try {
                       URL host = new URL(url);
                       requestBody.put("host", host.getHost());
                       requestBody.put("port", host.getPort());
                       String path = host.getPath().equals("") ? "/" : host.getPath();
                       requestBody.put("path", path);
                   } catch (MalformedURLException e) {
                       resp.put("status", "500");
                       return resp;
                   }
                   // 是有效的URL
               } catch (MalformedURLException e) {
                   // 不是有效的URL
                   resp.put("status", "500");
                   System.out.println("不是有效的URL");
                   return resp;
               }

        String re = kongAdmin.PatchService(serviceName, requestBody);
        JSONObject result = JSON.parseObject(re);
        return  result;
    }

    public String getIt(String url) {
        String re = kongAdmin.getIt(url);
        return re;
    }

    public String deleteService(String serviceName) {
        String re = kongAdmin.DeleteService(serviceName);
        return re;
    }
    public String deleteRoute(String serviceName, String routeName) {
        String re = kongAdmin.DeleteRoute(serviceName, routeName);
        return re;
    }
}
